<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>size.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_math_size.js.html">size.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview A utility class for representing two-dimensional sizes.
<a name="line17"></a> * @author brenneman@google.com (Shawn Brenneman)
<a name="line18"></a> */
<a name="line19"></a>
<a name="line20"></a>
<a name="line21"></a>goog.provide(&#39;goog.math.Size&#39;);
<a name="line22"></a>
<a name="line23"></a>
<a name="line24"></a>
<a name="line25"></a>/**
<a name="line26"></a> * Class for representing sizes consisting of a width and height. Undefined
<a name="line27"></a> * width and height support is deprecated and results in compiler warning.
<a name="line28"></a> * @param {number} width Width.
<a name="line29"></a> * @param {number} height Height.
<a name="line30"></a> * @constructor
<a name="line31"></a> */
<a name="line32"></a>goog.math.Size = function(width, height) {
<a name="line33"></a>  /**
<a name="line34"></a>   * Width
<a name="line35"></a>   * @type {number}
<a name="line36"></a>   */
<a name="line37"></a>  this.width = width;
<a name="line38"></a>
<a name="line39"></a>  /**
<a name="line40"></a>   * Height
<a name="line41"></a>   * @type {number}
<a name="line42"></a>   */
<a name="line43"></a>  this.height = height;
<a name="line44"></a>};
<a name="line45"></a>
<a name="line46"></a>
<a name="line47"></a>/**
<a name="line48"></a> * Compares sizes for equality.
<a name="line49"></a> * @param {goog.math.Size} a A Size.
<a name="line50"></a> * @param {goog.math.Size} b A Size.
<a name="line51"></a> * @return {boolean} True iff the sizes have equal widths and equal
<a name="line52"></a> *     heights, or if both are null.
<a name="line53"></a> */
<a name="line54"></a>goog.math.Size.equals = function(a, b) {
<a name="line55"></a>  if (a == b) {
<a name="line56"></a>    return true;
<a name="line57"></a>  }
<a name="line58"></a>  if (!a || !b) {
<a name="line59"></a>    return false;
<a name="line60"></a>  }
<a name="line61"></a>  return a.width == b.width &amp;&amp; a.height == b.height;
<a name="line62"></a>};
<a name="line63"></a>
<a name="line64"></a>
<a name="line65"></a>/**
<a name="line66"></a> * @return {!goog.math.Size} A new copy of the Size.
<a name="line67"></a> */
<a name="line68"></a>goog.math.Size.prototype.clone = function() {
<a name="line69"></a>  return new goog.math.Size(this.width, this.height);
<a name="line70"></a>};
<a name="line71"></a>
<a name="line72"></a>
<a name="line73"></a>if (goog.DEBUG) {
<a name="line74"></a>  /**
<a name="line75"></a>   * Returns a nice string representing size.
<a name="line76"></a>   * @return {string} In the form (50 x 73).
<a name="line77"></a>   * @override
<a name="line78"></a>   */
<a name="line79"></a>  goog.math.Size.prototype.toString = function() {
<a name="line80"></a>    return &#39;(&#39; + this.width + &#39; x &#39; + this.height + &#39;)&#39;;
<a name="line81"></a>  };
<a name="line82"></a>}
<a name="line83"></a>
<a name="line84"></a>
<a name="line85"></a>/**
<a name="line86"></a> * @return {number} The longer of the two dimensions in the size.
<a name="line87"></a> */
<a name="line88"></a>goog.math.Size.prototype.getLongest = function() {
<a name="line89"></a>  return Math.max(this.width, this.height);
<a name="line90"></a>};
<a name="line91"></a>
<a name="line92"></a>
<a name="line93"></a>/**
<a name="line94"></a> * @return {number} The shorter of the two dimensions in the size.
<a name="line95"></a> */
<a name="line96"></a>goog.math.Size.prototype.getShortest = function() {
<a name="line97"></a>  return Math.min(this.width, this.height);
<a name="line98"></a>};
<a name="line99"></a>
<a name="line100"></a>
<a name="line101"></a>/**
<a name="line102"></a> * @return {number} The area of the size (width * height).
<a name="line103"></a> */
<a name="line104"></a>goog.math.Size.prototype.area = function() {
<a name="line105"></a>  return this.width * this.height;
<a name="line106"></a>};
<a name="line107"></a>
<a name="line108"></a>
<a name="line109"></a>/**
<a name="line110"></a> * @return {number} The perimeter of the size (width + height) * 2.
<a name="line111"></a> */
<a name="line112"></a>goog.math.Size.prototype.perimeter = function() {
<a name="line113"></a>  return (this.width + this.height) * 2;
<a name="line114"></a>};
<a name="line115"></a>
<a name="line116"></a>
<a name="line117"></a>/**
<a name="line118"></a> * @return {number} The ratio of the size&#39;s width to its height.
<a name="line119"></a> */
<a name="line120"></a>goog.math.Size.prototype.aspectRatio = function() {
<a name="line121"></a>  return this.width / this.height;
<a name="line122"></a>};
<a name="line123"></a>
<a name="line124"></a>
<a name="line125"></a>/**
<a name="line126"></a> * @return {boolean} True if the size has zero area, false if both dimensions
<a name="line127"></a> *     are non-zero numbers.
<a name="line128"></a> */
<a name="line129"></a>goog.math.Size.prototype.isEmpty = function() {
<a name="line130"></a>  return !this.area();
<a name="line131"></a>};
<a name="line132"></a>
<a name="line133"></a>
<a name="line134"></a>/**
<a name="line135"></a> * Clamps the width and height parameters upward to integer values.
<a name="line136"></a> * @return {!goog.math.Size} This size with ceil&#39;d components.
<a name="line137"></a> */
<a name="line138"></a>goog.math.Size.prototype.ceil = function() {
<a name="line139"></a>  this.width = Math.ceil(this.width);
<a name="line140"></a>  this.height = Math.ceil(this.height);
<a name="line141"></a>  return this;
<a name="line142"></a>};
<a name="line143"></a>
<a name="line144"></a>
<a name="line145"></a>/**
<a name="line146"></a> * @param {!goog.math.Size} target The target size.
<a name="line147"></a> * @return {boolean} True if this Size is the same size or smaller than the
<a name="line148"></a> *     target size in both dimensions.
<a name="line149"></a> */
<a name="line150"></a>goog.math.Size.prototype.fitsInside = function(target) {
<a name="line151"></a>  return this.width &lt;= target.width &amp;&amp; this.height &lt;= target.height;
<a name="line152"></a>};
<a name="line153"></a>
<a name="line154"></a>
<a name="line155"></a>/**
<a name="line156"></a> * Clamps the width and height parameters downward to integer values.
<a name="line157"></a> * @return {!goog.math.Size} This size with floored components.
<a name="line158"></a> */
<a name="line159"></a>goog.math.Size.prototype.floor = function() {
<a name="line160"></a>  this.width = Math.floor(this.width);
<a name="line161"></a>  this.height = Math.floor(this.height);
<a name="line162"></a>  return this;
<a name="line163"></a>};
<a name="line164"></a>
<a name="line165"></a>
<a name="line166"></a>/**
<a name="line167"></a> * Rounds the width and height parameters to integer values.
<a name="line168"></a> * @return {!goog.math.Size} This size with rounded components.
<a name="line169"></a> */
<a name="line170"></a>goog.math.Size.prototype.round = function() {
<a name="line171"></a>  this.width = Math.round(this.width);
<a name="line172"></a>  this.height = Math.round(this.height);
<a name="line173"></a>  return this;
<a name="line174"></a>};
<a name="line175"></a>
<a name="line176"></a>
<a name="line177"></a>/**
<a name="line178"></a> * Scales this size by the given scale factors. The width and height are scaled
<a name="line179"></a> * by {@code sx} and {@code opt_sy} respectively.  If {@code opt_sy} is not
<a name="line180"></a> * given, then {@code sx} is used for both the width and height.
<a name="line181"></a> * @param {number} sx The scale factor to use for the width.
<a name="line182"></a> * @param {number=} opt_sy The scale factor to use for the height.
<a name="line183"></a> * @return {!goog.math.Size} This Size object after scaling.
<a name="line184"></a> */
<a name="line185"></a>goog.math.Size.prototype.scale = function(sx, opt_sy) {
<a name="line186"></a>  var sy = goog.isNumber(opt_sy) ? opt_sy : sx;
<a name="line187"></a>  this.width *= sx;
<a name="line188"></a>  this.height *= sy;
<a name="line189"></a>  return this;
<a name="line190"></a>};
<a name="line191"></a>
<a name="line192"></a>
<a name="line193"></a>/**
<a name="line194"></a> * Uniformly scales the size to fit inside the dimensions of a given size. The
<a name="line195"></a> * original aspect ratio will be preserved.
<a name="line196"></a> *
<a name="line197"></a> * This function assumes that both Sizes contain strictly positive dimensions.
<a name="line198"></a> * @param {!goog.math.Size} target The target size.
<a name="line199"></a> * @return {!goog.math.Size} This Size object, after optional scaling.
<a name="line200"></a> */
<a name="line201"></a>goog.math.Size.prototype.scaleToFit = function(target) {
<a name="line202"></a>  var s = this.aspectRatio() &gt; target.aspectRatio() ?
<a name="line203"></a>      target.width / this.width :
<a name="line204"></a>      target.height / this.height;
<a name="line205"></a>
<a name="line206"></a>  return this.scale(s);
<a name="line207"></a>};
</pre>


</body>
</html>
